package com.nageoffer.shortlink.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import com.nageoffer.shortlink.common.dto.admin.req.UserLoginReqDTO;
import com.nageoffer.shortlink.common.dto.admin.req.UserRegisterReqDTO;
import com.nageoffer.shortlink.common.dto.admin.req.UserUpdateReqDTO;
import com.nageoffer.shortlink.common.dto.admin.resp.UserActualRespDTO;
import com.nageoffer.shortlink.common.dto.admin.resp.UserGetRespDTO;
import com.nageoffer.shortlink.common.dto.admin.resp.UserLoginRespDTO;
import com.nageoffer.shortlink.admin.service.UserService;
import com.nageoffer.shortlink.common.convention.result.Result;
import com.nageoffer.shortlink.common.convention.result.Results;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/short-link/admin/user")
@RequiredArgsConstructor
public class UserController {
    private final UserService userService;
    @GetMapping("/{username}")
    public Result<UserGetRespDTO> getUserByName(@PathVariable("username") String username){
        UserGetRespDTO result = userService.getUserByName(username);
        return Results.success(result);
    }
    @GetMapping("/has-username")
    public Result<Boolean> hasUsername(@RequestParam("username") String username){
        return Results.success(userService.hasUsername(username));
    }
    @PostMapping
    public Result<Void> register(@RequestBody UserRegisterReqDTO requestParam){
        userService.register(requestParam);
        return Results.success();
    }
    @GetMapping("/actual/{username}")
    public Result<UserActualRespDTO> getActualUserByUsername(@PathVariable("username") String username) {
        return Results.success(BeanUtil.toBean(userService.getUserByUsername(username), UserActualRespDTO.class));
    }
    @PutMapping
    public Result<Void> updateInfo(@RequestBody UserUpdateReqDTO requestParam){
        userService.updateInfo(requestParam);
        return Results.success();
    }
    @PostMapping("/login")
    public Result<UserLoginRespDTO> login(@RequestBody UserLoginReqDTO requestParam){
        return Results.success(userService.login(requestParam));
    }
    @GetMapping("/check-login")
    public Result<Boolean> checkLogin(@RequestParam String username,@RequestParam String token){
        return Results.success(userService.checkLogin(username,token));
    }
    @DeleteMapping("/logout")
    public Result<Void> logout(@RequestParam String username,@RequestParam String token){
        userService.logout(username,token);
        return Results.success();
    }

}
